package de.dopichaj.labrador.index.backend;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

public final class RAMInvertedList implements InvertedList {

    private static final int SEPARATOR = 0;
    private final List<TermFrequency> list = new ArrayList<TermFrequency>();
    private boolean isSorted = true;
    static final int MOD = 0x80;
    
    public void addTermFrequency(TermFrequency frequency) {
        list.add(frequency);
        isSorted = false;
    }

    public void sort(Dictionary dictionary) {
        Collections.sort(list);
        isSorted = true;
    }
    
    public void sort() {
        sort(null);
    }

    public Iterator<TermFrequency> iterator() {
        return list.iterator();
    }

    public void write(final DataOutputStream stream) throws IOException {
        assert isSorted;
        
        int prevTermID = -1;
        for (final TermFrequency freq : list) {
            if (freq.getTermID() != prevTermID) {
                writeVariableLengthInt(stream, SEPARATOR);
                writeVariableLengthInt(stream, freq.getTermID());
            }
            writeVariableLengthInt(stream, freq.getDocumentID() + 1);
            writeVariableLengthInt(stream, freq.getFrequency());
            prevTermID = freq.getTermID();
        }
    }

    public void read(final DataInputStream stream) throws IOException {

        int termID = -1;
        while (stream.available() > 0) {
            int value = readVariableLengthInt(stream);
            if (value == SEPARATOR) {
                termID = readVariableLengthInt(stream);
                value = readVariableLengthInt(stream);
            }
            final int documentID = value - 1;
            final int frequency = readVariableLengthInt(stream);
            addTermFrequency(new TermFrequency(termID, documentID, frequency));
        }
    }

    private void writeVariableLengthInt(final OutputStream stream,
        final int value) throws IOException {
        
        int v = value;
        while (v >= MOD) {
            int b = MOD | (v % MOD);
            stream.write(b);
            v /= MOD;
        }
        stream.write(v);
    }

    private static int readVariableLengthInt(InputStream in)
        throws IOException {
    
        int v = 0;
        int f = 1;
        int r;
        do {
            r = in.read();
            if (r == -1) {
                throw new IOException("Premature end of file");
            }
            v += f * (r % MOD);
            f *= MOD;
        } while ((r & MOD) != 0);
        return v;
    }

    public List<TermFrequency> search(int termID) {
        final List<TermFrequency> results = new ArrayList<TermFrequency>();
        for (final TermFrequency freq : list) {
            if (freq.getTermID() == termID) {
                results.add(freq);
            }
        }
        return results;
    }

    public void close() throws IOException {
        // nothing to do
    }
}
/*
Copyright (c) 2007 Philipp Dopichaj

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/